Ext.define('PJ.view.setassessmentpaper.List', {
    extend: 'AM.default.DefaultList',
    requires: ['AM.default.DefaultSearchForm', 
               'AM.default.DateTime', 
               'AM.default.DefaultDateField',
               'AM.default.jsonp.DefaultSubmitJsonp',
               'AM.default.jsonp.DefaultLoadJsonp',
               'PJ.view.setassessmentpaper.SelectStaffRightFrame',//设置问卷
               'PJ.view.setassessmentpaper.ConfigureCourseList'//配置课程评估问卷
               ],
    
    initComponent: function(){
    	var me = this;
        me.columns = [{
            header: '培训班编号',
            dataIndex: 'tc.class_Num',
            width: 90,
			renderer: function(value, metaData, record){
				metaData.tdAttr = 'data-qtip="' + value + '"';
				return value;
			}
        }, {
            header: '培训班名称',
            dataIndex: 'tc.class_Name',
            flex: 1,
            minWidth: 120,
			renderer: function(value, metaData, record){
				metaData.tdAttr = 'data-qtip="' + value + '"';
				return value;
			}
        }, {
            header: '主办单位',
            dataIndex: 'tc.sponsor_Name',
            width: 120,
			renderer: function(value, metaData, record){
				metaData.tdAttr = 'data-qtip="' + value + '"';
				return value;
			}
        }, {
            header: '开班时间',
            dataIndex: 'tc.begin_Date',
            width: 90,
            renderer: function(value, metaData, record){
                return value;
            }
        }, {
            header: '结业时间',
            dataIndex: 'tc.end_Date',
            width: 90,
            renderer: function(value, metaData, record){
                return value;
            }
        },{
            header: '评估开始时间',
            dataIndex: 'tc.assess_start_time',
            width: 90,
            renderer: function(value, metaData, record){
                return value;
            }
        }, {
            header: '评估结束时间',
            dataIndex: 'tc.assess_end_time',
            width: 90,
            renderer: function(value, metaData, record){
                return value;
            }
        }, {
            header: '已通过人数',
            dataIndex: 'reportedNum',
            width: 70,
            renderer: function(value, metaData, record){
            	return  value;
            }
        }/*, {
        	header: '二维码',
            dataIndex: 'registrationExplanation',
            width: 65,
            align: 'center',
            renderer: function(value, metaData, record){
            	metaData.tdAttr = 'classNum="' + record.get("q.classNum") + '" subjectTitle="' + record.get("tc.class_Name") + '"';
            	return '<span class="QRcodeIco" style="cursor: pointer;"></span> ';
            },
            listeners: {
	            click: function(g, clm){
	            	var subjectTitle = clm.getAttribute("subjectTitle");
            		Ext.create('AM.default.DefaultWindowView', {
            			title: '二维码',
            			width: 270,
            			height: 330,
            			grid: me,
            			classNum: clm.getAttribute("classNum"),
            			items: [{
            				html: '<div style="text-align: center;padding-top:10px;"><span color="#A9A9A9">' + subjectTitle + '</span></div><img src="./erweima/classErweima/班级ID.png" alt="二维码" style="width:255px;height:255px;padding:8px 20px 20px 20px;">'
            			}],
            			buttons: [{
            	            text: '下载',
            	            scope: this,
            	            handler: function(button, event){
            	                //var win = button.up('window');
            	            }
            	        }, {
            	            text: '取消',
            	            scope: this,
            	            handler: function(button, event){
            	                var win = button.up('window');
            	                win.close();
            	            }
            	        }]
            		}).show();
	            }
            }
        }*/,{
        	header: '二维码',
            dataIndex: 'registrationExplanation',
            width: 65,
            align: 'center',
            sortable: false,
            renderer: function(value, metaData, record){
            	metaData.tdAttr = 'classNum="' + record.get("tc.class_Num") + '" classId="' + record.get("classId") + '" className="' + record.get("tc.class_Name") + '"';
            	return '<span class="QRcodeIco" style="cursor: pointer;"></span> ';
            },
            listeners: {
	            click: function(g, clm){
	            	var classId = clm.getAttribute("classId");
	            	var className = clm.getAttribute("className");
	            	var src = me.store.qrCodeUrl + "?classId=" + classId;
            		Ext.create('AM.default.DefaultWindowView', {
            			title: '二维码',
            			width: 600,
            			height: 580,
            			grid: me,
            			html: "<iframe src=\"" + src + "\" frameborder='0' style=\"width:100%;height:100%\"></iframe>",
            			/*items: [{
            				html: '<div style="text-align: center;padding-top:10px;"><span color="#A9A9A9">' + className + '</span></div><img src="./skin/red/images/ztyx.png" alt="二维码" style="width:255px;height:255px;padding:8px 20px 20px 20px;">'
            			}],*/
            			buttons: [{
            	            text: '关闭',
            	            scope: this,
            	            handler: function(button, event){
            	                var win = button.up('window');
            	                win.close();
            	            }
            	        }]
            		}).show();
	            }
            }
        },{
        	header: '问卷预览',
            dataIndex: 'registrationExplanation',
            width: 65,
            align: 'center',
            sortable: false,
            renderer: function(value, metaData, record){
            	metaData.tdAttr = 'classNum="' + record.get("tc.class_Num") + '" classId="' + record.get("classId") + '" className="' + record.get("tc.class_Name") + '"';
            	return '<span class="classQuestionnairePreviewIco" style="cursor: pointer;"></span> ';
            },
            listeners: {
	            click: function(g, clm){
	            	var className = clm.getAttribute("className");
	            	var src = me.store.qPreviewUrl + "?className=" + className;
            		Ext.create('AM.default.DefaultWindowView', {
            			title: '问卷预览',
            			width: 575,
            			height: 580,
            	        autoScroll:false,
            			grid: me,
            			html: "<iframe src=\"" + src + "\" frameborder='0' style=\"width:100%;height:100%\"></iframe>",
            			buttons: [{
            	            text: '关闭',
            	            scope: this,
            	            handler: function(button, event){
            	                var win = button.up('window');
            	                win.close();
            	            }
            	        }]
            		}).show();
	            }
            }
        }];
        me.store = Ext.create('PJ.store.setassessmentpaper.List', {
            isLoad: false
        });
        this.loadType = gj_onlineLearning_constant.loadType;//加载方式
        me.callParent(arguments);
    },
    
    //配置问卷
    onConfigureClick: function(button){
        var me = this;
        var records = me.getSelectionModel().getSelection();
        if (records.length > 1) {
            Ext.Msg.show({
                title: '警告',
                msg: '操作时只可选中一条记录！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return;
        }

    	var me = this;
    	var records = me.getSelectionModel().getSelection();
    	win = Ext.create('PJ.view.setassessmentpaper.SelectStaffRightFrame',{
    		title: "配置评估问卷",
    		grid: me,
    		classId:records[0].get("classId")
    	}).show();
    	var actionName = "load";//默认使用非JSONP的加载方法名“load”
        if(gj_onlineLearning_constant.loadType=="jsonp") {
        	actionName= "loadJsonp";
        }
        win.down('form').getForm().doAction(actionName,{
        	url: me.store.preUpdateUrl,
            params: {
            	classId: records[0].get("classId")//培训班ID
            },
            success:function(form,action){
            	var data = action.response.responseText;
    			var json = gj_onlineLearning_constant.loadType=="ajax" ? Ext.decode(data, true) : action.response;
    			var form = win.down('form');	 
    			var baseForm = form.getForm();
    			var assessStartDay = json.data.assessStartDay;
    			var assessEndDay = json.data.assessEndDay;
    				baseForm.findField("assessStartTime").setValue(assessStartDay);//结束前
    				baseForm.findField("assessEndTime").setValue(assessEndDay);//结束后
            }
        });
    },
    //配置问卷--确定事件
    configureSub: function(){
        var me = this;
        var grid = Ext.ComponentQuery.query("celaConfigurePaperList")[0];
        var win = grid.up('window');
        var form = win.down('form');
        var record = grid.getSelectionModel().getSelection();
        if(record.length == 0){
            Ext.Msg.show({
                title: '警告',
                msg: '请选择要操作的数据！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return;
        }else if (record.length > 1) {
            Ext.Msg.show({
                title: '警告',
                msg: '操作时只可选中一条记录！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return;
        }
        var classId = record[0].get("classId");
        if(gj_onlineLearning_constant.loadType == "jsonp"){
			var store = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
				loadType: gj_onlineLearning_constant.loadType,
				url: me.store.subUrl,
				extraParams: {
					classId: classId
				}
			});
			store.load(function(rec, operation, success){
				Ext.getCmp("loadingMask").hide();
				if(success){
					Orchid.alert('消息', '配置成功！', function(){
						win.close();
						var store = me.getStore();
						store.load();
					});
				}else{
					Orchid.alert('消息', operation.error);
				}
			});
        }else if(gj_onlineLearning_constant.loadType == "ajax"){
            Ext.Ajax.request({
                url: me.store.subUrl,
                params: {
                	classId: classId
                },
                success: function(response){
                    var data = response.responseText;
                    var json = Ext.decode(data, true);
                    if (json.success) {
                    	Orchid.alert('消息', '配置成功！', function(){
    						win.close();
                            var store = me.getStore();
                            store.reload();
                        });
                    }
                    else {
                        Orchid.alert('消息', json.msg, function(){
                            var store = me.getStore();
                            store.reload();
                        });
                    }
                }
            });
        }
    },
    /**
     * 配置课程评估问卷
     */
    onConfigureCourseClick: function(){
    	var me = this;
    	var records = me.getSelectionModel().getSelection();
        if (records.length > 1) {
            Ext.Msg.show({
                title: '警告',
                msg: '操作时只可选中一条记录！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return;
        }else if(records[0].get("configQuestionnaireState")==2){
        	 Ext.Msg.show({
                 title: '警告',
                 msg: '请先配置班级评估问卷！',
                 icon: Ext.Msg.WARNING,
                 buttons: Ext.Msg.OK
             });
             return;
        }else{
        	Ext.create('AM.default.DefaultWindowView',{
        		title: "配置课程评估问卷",
        		grid: me,
        		height: 400,
        		width: 900,
        		searchClassId: records[0].get("classId"),
        		surveyObjectIds: null,
        		items:[{
                    xtype: 'configureCourseList'
                }]
        	});
        }
    },
    /**
     * 设置评估方式
     */
    onQuestionnaireMethod: function(){
    	var me = this;
    	var records = me.getSelectionModel().getSelection();
    	var searchClassIds=new Array();
    	var nowDate = new Date(new Date().getFullYear()+"/"+(new Date().getMonth()+1)+"/"+new Date().getDate()+" 08:00:00");
    	for(i of records){
    		var assDate=new Date(new Date(i.get('tc.assess_start_time').replace(/-/g,'/')));
    		if(assDate.getTime()<=nowDate.getTime()){
    			Orchid.alert('提示','不允许操作已经评估开始的班级！');
    			return;
    		}
    		searchClassIds.push(i.get("classId"));
    	}
    	Ext.create('AM.default.DefaultWindowView',{
			title: "设置评估方式",
    		height: 100,
    		width: 200,
    		grid:me,
    		searchClassIds: searchClassIds, 
            items:[{
                xtype: "radiogroup",
                id:"QMethodID",
                items: [
                    { boxLabel: "实名", name: "QMethod", inputValue: "1",checked:true },
                    { boxLabel: "匿名", name: "QMethod", inputValue: "2" }
                ]
            }],
    	    buttons: [{
    	        text: '确定',
    	        handler: function(){
    	        	var grid=this.up("window");
    	        	var qm=Ext.getCmp('QMethodID').getChecked()[0].inputValue;
    	        	Ext.Ajax.request({
						  url:grid.grid.store.qMethodUrl,
						  params: {
							  searchClassIds: grid.searchClassIds,
							  questionnaireMethod: qm
	                       },
						  success:function(response){
							  var data=response.responseText;
							  var json=Ext.decode(data,true);
							 Orchid.alert('提示',json.message,function(){
								 grid.grid.store.reload();
								 grid.close();
							  });
						  }
					  });
    	        }
    	    },{
    	        text: orchid_lang.buttons.closeButtonText,
    	        handler: function(){
    	            this.up("window").close();
    	        }
    	    }]
		});
    },
    /**
     * 设置评估时间
     */
    onSetEvaluationTimeClick: function(){
    	var me = this;
    	var records = me.getSelectionModel().getSelection();
        if (records.length > 1) {
            Ext.Msg.show({
                title: '警告',
                msg: '操作时只可选中一条记录！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return;
        }

    	var records = me.getSelectionModel().getSelection();
    	win = Ext.create('PJ.view.setassessmentpaper.SetEvaluationTime',{
    		title: "配置评估时间",
    		height: 120,
    		width: 300,
    		grid: me,
    		classId:records[0].get("classId")
    	}).show();
    	var actionName = "load";//默认使用非JSONP的加载方法名“load”
        if(gj_onlineLearning_constant.loadType=="jsonp") {
        	actionName= "loadJsonp";
        }
        win.down('form').getForm().doAction(actionName,{
        	url: me.store.preUpdateUrl,
            params: {
            	classId: records[0].get("classId")//培训班ID
            },
            success:function(form,action){
            	var data = action.response.responseText;
    			var json = gj_onlineLearning_constant.loadType=="ajax" ? Ext.decode(data, true) : action.response;
    			var form = win.down('form');	 
    			var baseForm = form.getForm();
    			var assessStartDay = json.data.assessStartDay;
    			var assessEndDay = json.data.assessEndDay;
    				baseForm.findField("assessStartTime").setValue(assessStartDay);//结束前
    				baseForm.findField("assessEndTime").setValue(assessEndDay);//结束后
            }
        });
    },
    listeners: {
    	selectionchange: function(selModel, selected){
            this.down('#' + this.oldId + "Configure").setDisabled(selected.length === 0);
            this.down('#' + this.oldId + "ConfigureCourse").setDisabled(selected.length === 0);
            this.down('#' + this.oldId + "QuestionnaireMethod").setDisabled(selected.length === 0);
            //this.down('#' + this.oldId + "SetEvaluationTime").setDisabled(selected.length === 0);
        },
    	viewready: function(){
    		var me = this;
    		me.addDocked([{
    			dock: 'top',
    			xtype: "searchform",
    			columnCount: 3,
    			fieldDefaults: {
    				labelAlign: 'right',
    				labelWidth: 70
    			},
    			items: [{
    				xtype: "textfield",
    				fieldLabel: '培训班编号',
    				name: "searchClassNum",
    				maxLength: "50",
    				emptyText: "请输入培训班编号"
    			}, {
    				xtype: "textfield",
    				fieldLabel: '培训班名称',
    				name: "searchClassName",
    				maxLength: "20",
    				emptyText: "请输入培训班名称"
    			}, {
    				xtype: "textfield",
    				fieldLabel: '主办单位',
    				name: "searchSponsorName",
    				maxLength: "20",
    				emptyText: "请输入主办单位"
    			}, {
    				xtype: 'defaultDate',
    				fieldLabel: '开班时间',
    				name: 'searchBeginDate',
    				id: 'searchQuestionnaireSD',
    				vtype: 'startLessEndDate',
    				startLessEndDate: {
    					targetCmpId: 'searchQuestionnaireED',
    					isStart: true
    				},
    				emptyText: "请输入开班时间"
    			}, {
    				xtype: 'defaultDate',
    				fieldLabel: '结业时间',
    				name: 'searchEndDate',
    				id: 'searchQuestionnaireED',
    				vtype: 'startLessEndDate',
    				vtypeText: '结业时间不能早于开班时间！',
    				startLessEndDate: {
    					targetCmpId: 'searchQuestionnaireSD',
    					isStart: false
    				},
    				emptyText: "请输入结业时间"
    			}],
    			grid: this
    		}, {
                xtype: 'toolbar',
                dock: 'top',
                displayInfo: true,
                items: [{
                    text: '配置培训班评估问卷',
                    iconCls: 'publishIco',
                    itemId: this.oldId + "Configure",
                    disabled: true, 
                    scope: this,
                    handler: this.onConfigureClick
                },'-',{
                    text: '配置课程评估问卷',
                    iconCls: 'publishIco',
                    itemId: this.oldId + "ConfigureCourse",
                    disabled: true, 
                    scope: this,
                    handler: this.onConfigureCourseClick
                },'-',{
                    text: '配置班级问卷评估方式',
                    iconCls: 'publishIco',
                    itemId: this.oldId + "QuestionnaireMethod",
                    disabled: true, 
                    scope: this,
                    handler: this.onQuestionnaireMethod
                }/*,'-',{
                    text: '设置评估时间',
                    iconCls: 'publishIco',
                    itemId: this.oldId + "SetEvaluationTime",
                    disabled: true, 
                    scope: this,
                    handler: this.onSetEvaluationTimeClick
                }*/, "->", this.countSelectPanel]
            }]);
            var store = this.store;
            var params = store.proxy.extraParams;
            var newParams = {};
            Ext.apply(params, newParams);
            store.isLoad = true;
            store.load();
    		// 触发权限过滤事件
    		this.fireEvent("permission", this);
    	}
    }
});

